package com.gopay.common.cps.dao.global.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.global.GopayGlobalVariablesDao;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author LEI XIAOBAO
 * @date 2016年1月29日
 */
@Repository("gopayGlobalVariablesDao")
public class GopayGlobalVariablesDaoImpl extends BaseDAOImpl<Serializable, Serializable> implements GopayGlobalVariablesDao {

	@Override
	public String getGoPayGlobalVariablesByParamKey(String paramKey) {
		String sql = "select v.param_value from gopay_global_variables v where v.param_key=:paramKey1";
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("paramKey1", paramKey);
		List<?> list = findBySql(sql, param);
		if( list != null && list.size() > 0 ){
			return list.get(0).toString();
		}
		return null;
	}

	@Override
	public List<?> getGopayGlobalVariablesListByCondition(Map<String, Object> params) {
		String sql = "select * from gopay_global_variables v where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			sql += " AND v."+entry.getKey()+"=:"+entry.getKey();
		}
		List<?> list = findBySql(sql, params);
		if( list != null && list.size() > 0 ){
			return list;
		}
		return null;
	}

	@Override
	public int queryGopayGlobalVariablesCount(Map<String, Object> paramMap) {
		StringBuffer hql = new StringBuffer(
				"select count(*) from GopayGlobalVariables  where 1=1");
		Map<String, Object> param = new HashMap<String, Object>();
		if (paramMap != null) {
			if (paramMap.get("titleQuery") != null) {
				hql.append(" and title like :titleQuery");
				param.put("titleQuery", paramMap.get("titleQuery"));
			}
			if (paramMap.get("paramKeyQuery") != null) {
				hql.append(" and paramKey like :paramKeyQuery");
				param.put("paramKeyQuery", paramMap.get("paramKeyQuery"));
			}
			if (paramMap.get("groupsQuery") != null) {
				hql.append(" and groups like :groupsQuery");
				param.put("groupsQuery", paramMap.get("groupsQuery"));
			}
		}
		return this.findCountByHql(hql.toString(), paramMap);
	}

	@Override
	public List<?> queryGopayGlobalVariables(Map<String, Object> paramMap, Integer page, Integer rowNum) {
		StringBuffer hql = new StringBuffer(
				"from GopayGlobalVariables  where 1=1");
		Map<String, Object> param = new HashMap<String, Object>();
		if (paramMap != null) {
			if (paramMap.get("titleQuery") != null) {
				hql.append(" and title like :titleQuery");
				param.put("titleQuery", paramMap.get("titleQuery"));
			}
			if (paramMap.get("paramKeyQuery") != null) {
				hql.append(" and paramKey like :paramKeyQuery");
				param.put("paramKeyQuery", paramMap.get("paramKeyQuery"));
			}
			if (paramMap.get("groupsQuery") != null) {
				hql.append(" and groups like :groupsQuery");
				param.put("groupsQuery", paramMap.get("groupsQuery"));
			}
			if (paramMap.get("id") != null) {
				hql.append(" and id = :id");
				param.put("id", paramMap.get("id"));
			}
		}

		hql.append(" order by createTime desc");

		return this.findByHql(hql.toString(), param, page, rowNum);
	}

	@Override
	public List getGopayGlobalVariablesList(Map<String, Object> params) {
		String sql = "from GopayGlobalVariables v  where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			sql += " AND v."+entry.getKey()+"=:"+entry.getKey();
		}
		List<?> list = findByHql(sql, params);
		if( list != null && list.size() > 0 ){
			return list;
		}
		return null;
	}

}
